package com.example.exp_7;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;
import java.util.Map;

public class Staff_info extends AppCompatActivity {
    EditText ed_id,ed_name,ed_age,ed_height;
    Button btn_add,btn_delete,btn_update,btn_display;
    ListView staff_info;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staff_info);
        ed_id = findViewById(R.id.ed_id);
        ed_name = findViewById(R.id.ed_name);
        ed_age = findViewById(R.id.ed_age);
        ed_height = findViewById(R.id.ed_height);
        btn_add = findViewById(R.id.btn_add);
        btn_delete = findViewById(R.id.btn_delete);
        btn_update = findViewById(R.id.btn_update);
        btn_display = findViewById(R.id.btn_display);
        staff_info = findViewById(R.id.lv_staff);

        View view = LayoutInflater.from(this).inflate(R.layout.staff_info_head, null);
        staff_info.addHeaderView(view);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Staff staff = new Staff(ed_id.getText().toString(),ed_name.getText().toString(),ed_age.getText().toString(),ed_height.getText().toString());
                StaffBpo.insert(getApplicationContext(),staff);
                cleanEd();
                display();
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = ed_id.getText().toString();
                StaffBpo.delete(getApplicationContext(),id);
                cleanEd();
                display();
            }
        });
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Staff staff = new Staff(ed_id.getText().toString(),ed_name.getText().toString(),ed_age.getText().toString(),ed_height.getText().toString());
                StaffBpo.update(getApplicationContext(),staff);
                cleanEd();
                display();
            }
        });
        btn_display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cleanEd();
                display();
            }
        });
        staff_info.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView id = view.findViewById(R.id.tv_id);
                TextView name = view.findViewById(R.id.tv_name);
                TextView age = view.findViewById(R.id.tv_age);
                TextView height = view.findViewById(R.id.tv_height);
                ed_id.setText(id.getText().toString());
                ed_name.setText(name.getText().toString());
                ed_age.setText(age.getText().toString());
                ed_height.setText(height.getText().toString());
            }
        });
    }
    public void cleanEd() {
        ed_id.setText("");
        ed_name.setText("");
        ed_age.setText("");
        ed_height.setText("");
    }
    public void display() {
        List<Map<String,String>> staffList = StaffBpo.display(getApplicationContext());
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,staffList,R.layout.staff_single_info,new String[]{"id","name","age","height"},new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_age,R.id.tv_height});
        staff_info.setAdapter(simpleAdapter);
    }
}
